<template>
  <el-dialog title="上传文件" :close-on-click-modal="false" @close="closeHandle" :visible.sync="visible">
    <el-upload drag :action="url" :before-upload="beforeUploadHandle" :on-success="successHandle" multiple
      :file-list="fileList" style="text-align: center;">
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip">图片支持jpg、png、gif格式</div>
      <div class="el-upload__tip" slot="tip">文档支持doc、docx、pdf、txt格式</div>
      <div class="el-upload__tip" slot="tip">表格支持xls、xlsx格式</div>
      <div class="el-upload__tip" slot="tip">视频支持mp4、avi、flv、mkv格式</div>
    </el-upload>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        url: '',
        num: 0,
        successNum: 0,
        fileList: []
      }
    },
    methods: {
      init () {
        this.visible = true
        this.url = this.$http.adornUrl('/admin-interfaces/file/upload')
      },
      // 上传之前
      beforeUploadHandle (file) {
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif' &&
          file.type !== 'application/msword' && file.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' &&
          file.type !== 'application/pdf' && file.type !== 'text/plain' &&
          file.type !== 'application/vnd.ms-excel' && file.type !== 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' &&
          file.type !== 'video/mp4' && file.type !== 'video/avi' && file.type !== 'video/flv' && file.type !== 'video/mkv') {
          this.$message({
            showClose: true,
            message: '文件格式不符合要求',
            type: 'warning',
            center: true
          })
          return false
        }
        this.num++
      },
      // 上传成功
      successHandle (response, file, fileList) {
        this.fileList = fileList
        this.successNum++
        if (response && response.code === 200) {
          if (this.num === this.successNum) {
            this.$message({
              message: '上传成功',
              type: 'success',
              duration: 1000
            })
          }
        } else {
          this.$message.error(response.message)
        }
      },
      // 弹窗关闭时
      closeHandle () {
        this.fileList = []
        this.$emit('refreshDataList')
      }
    }
  }
</script>
